package com.mlink.vs.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccMessage;
import com.mlink.vs.entity.PiccTaskPool;
import com.mlink.vs.entity.PiccUser;
import com.mlink.vs.service.PiccMessageService;
import com.mlink.vs.service.PiccSeatStatusService;
import com.mlink.vs.service.PiccTaskPoolService;
import com.mlink.vs.service.PiccTaskStatusService;
import com.mlink.vs.service.PiccUserService;
import com.mlink.vs.util.Constants;
import com.mlink.vs.util.StreamUtil;

public class SocketAction {

	private static final String Boolean = null;

	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	PiccTaskPoolService piccTaskPoolService;
	
	@Resource
	PiccTaskStatusService piccTaskStatusService;
	
	@Resource
	PiccMessageService piccMessageService;
	
	@Resource
	PiccUserService piccUserService;
	
	//登陆监听
	@Execute(validator = false)
	public String notice(){
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		Map<String, String> stsMap = new HashMap<String, String>();

		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String operate = (String) mapData.get("operate");
		String userId = (String) mapData.get("userId");
	    Object status =  mapData.get("status");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(operate)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "operate不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(userId)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "userId不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(status.toString())) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "status不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if(operate.equals("online")){
		
				String line = "0";
				if (status.toString().equals("true")){
					//上线
					line = "1";
				}else{
					//下线，删除正在排队的单子
					String taskType=Constants.TASK_TYPE_VIDEO;
					String taskStatus=Constants.TASK_STATUS_DAICL;
					List<PiccTaskPool> piccTaskPoolList = piccTaskPoolService.findDiaoDan(userId,taskType,taskStatus);
					if(null!=piccTaskPoolList && piccTaskPoolList.size()>0){
						for (int i = 0; i < piccTaskPoolList.size(); i++) {
							  PiccTaskPool piccTaskPool = piccTaskPoolList.get(i);
							  String taskID=piccTaskPool.taskId;
							  piccTaskPoolService.deleteDiaodan(taskID);
							  piccTaskStatusService.deleteDiaodan(taskID);
						}
					}
				}
				
				piccUserService.updateStatus(line,userId);
				stsMap.put("sts", "0");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}else{
				stsMap.put("sts", "2");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	//聊天记录监听
	@Execute(validator = false)
	public String groupChat(){
		
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		Map<String, String> stsMap = new HashMap<String, String>();

		Map<String, String> mapData = gson.fromJson(indata.toString(),Map.class);
		if (mapData == null)
			return null;
		
		String type = (String) mapData.get("type");
		String info = (String) mapData.get("info");
		String userId = (String) mapData.get("userId");
		String userName = (String) mapData.get("userName");
		String tos = (String) mapData.get("to");
		String group = (String) mapData.get("group");
	
		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(type)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "type不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(info)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "info不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(userId)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "userId不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(userName)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "userName不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(tos)) {
				stsMap.put("sts", "1");
				stsMap.put("msg", "to不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			String[] to = tos.split(",");
			for (int i = 0; i < to.length; i++) {
				String a=to[i];
				PiccMessage piccMessage = new PiccMessage();
				piccMessage.id=UUID.randomUUID().toString().replace("-", "");
				piccMessage.senderId=userId;
				piccMessage.senderName=userName;
				PiccUser piccUser = piccUserService.findById(a);
				piccMessage.receiverId=a;
				piccMessage.receiverName=piccUser.userName;
				piccMessage.msgType=type;
				piccMessage.content=info;
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				piccMessage.signinDate=formatter.format(new Timestamp((new Date()).getTime()));
				piccMessage.isRead = "0";
				piccMessage.groupName=group;
				piccMessageService.insert(piccMessage);
			}
			stsMap.put("sts", "0");
			rep.getWriter().println(gson.toJson(stsMap));
			rep.getWriter().flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}